package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.PayWayListModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class PayTypeAdapter extends ItemAdapter<PayWayListModel.DatasBean, PayTypeAdapter.DefaultHolder> {


    private Context context;
    private int selectPoi = -1;

    public PayTypeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_pay_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        PayWayListModel.DatasBean item = getItem(position);

            Glide.with(context).load(item.icon).into(holder.imgType);
            holder.txtType.setText(item.name);
            if (selectPoi == position) {
                holder.checkBox.setImageResource(R.mipmap.img_check_box_check);
            } else {
                holder.checkBox.setImageResource(R.mipmap.img_check_box_uncheck);
            }
            holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPoi = position;
                    if (onChooseListener != null) {
                        onChooseListener.onPayTypeSelect(item.id, item.en_name);
                    }
                    notifyDataSetChanged();
                }
            });


    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_type)
        ImageView imgType;
        @BindView(R.id.txt_type)
        TextView txtType;
        @BindView(R.id.check_box)
        ImageView checkBox;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public int getSelectPoi() {
        return selectPoi;
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onPayTypeSelect(String id, String name);

    }
}
